﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Interfaces;

namespace ShopB2C.SQLDataAccess {
    public class Module : IModule {

        #region Const Variables

        private const string _moduleAppend = "module_append";
        private const string _moduleUpdate = "module_Update";
        private const string _moduleDelete = "module_Delete";
        private const string _moduleSelect = "module_Select";
        private const string _moduleSelectList = "module_SelectList";

        #endregion

        public Module(string connectionString) {
            DbBase.ConnectionString = connectionString;
        }

        #region Module

        public void ModuleAppend(ModuleEntity cme) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@Code", 20, cme.Code).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description", 250, cme.Description).SqlDbType = SqlDbType.NVarChar;
            SqlDbProcedure.ExecuteNonQuery(_moduleAppend, sqlParams.ToArray);
        }

        public void ModuleUpdate(ModuleEntity cme) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@ModuleId", cme.ModuleId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Code", 20, cme.Code).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description", 250, cme.Description).SqlDbType = SqlDbType.NVarChar;
            SqlDbProcedure.ExecuteNonQuery(_moduleUpdate, sqlParams.ToArray);
        }

        public void ModuleDelete(string moduleIds) {
            SqlDbProcedure.ExecuteNonQuery(_moduleDelete,
                DbParams<SqlParameter>.CreateParameter<string>("@ModuleIds", moduleIds));
        }

        public ModuleEntity GetClassModuleById(int moduleId) {
            ModuleEntity cme = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_moduleSelect,
                DbParams<SqlParameter>.CreateParameter<int>("@ModuleId", moduleId));
            if (dr.HasRows) {
                if (dr.Read()) {
                    cme = new ModuleEntity() {
                        ModuleId = (int)dr["ModuleId"],
                        Code = (string)dr["Code"],
                        Description = dr["Description"].ToString()
                    };
                }
            }
            dr.Close();
            dr.Dispose();
            return cme;
        }

        public List<ModuleEntity> GetClassModuleList(int pageSize, int pageIndex, out int pageCount) {
            pageCount = 0;
            List<ModuleEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@PageCount", ParameterDirection.Output, 0).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageSize", pageSize).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageIndex", pageIndex).SqlDbType = SqlDbType.Int;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_moduleSelectList, sqlParams.ToArray);
            if (dr.HasRows) {
                listResult = new List<ModuleEntity>();
                while (dr.Read()) {
                    pageCount = (int)dr["PageCount"];
                    listResult.Add(new ModuleEntity() {
                        ModuleId = (int)dr["ModuleId"],
                        Code = (string)dr["Code"],
                        Description = dr["Description"].ToString()
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        #endregion
    }
}
